package com.wgc.search.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    //队列
    public static final String RABBIT_QUEUE = "rabbit.queue";
  public static final String GOODS_QUEUE = "goods.queue";
 public static final String GOODS_DOWN_QUEUE = "goods.down.queue";
    //交换机
    public static final String RABBIT_EXCHANGE = "rabbit.exchange";
public static final String GOODS_EXCHANGE = "goods.exchange";
public static final String GOODS_DOWN_EXCHANGE = "goods.down.exchange";

    //声明队列
    /*
     *   new Queue(QUEUE_EMAIL,true,false,false)
     *   durable="true" 持久化 rabbitmq重启的时候不需要创建新的队列
     *   auto-delete 表示消息队列没有在使用时将被自动删除 默认是false
     *   exclusive  表示该消息队列是否只在当前connection生效,默认是false
     */
    @Bean(RABBIT_QUEUE)
    public Queue RABBIT_QUEUE() {
        return new Queue(RABBIT_QUEUE);
    }
@Bean(GOODS_QUEUE)
    public Queue GOODS_QUEUE() {
        return new Queue(GOODS_QUEUE);
    }
@Bean(GOODS_DOWN_QUEUE)
    public Queue GOODS_DOWN_QUEUE() {
        return new Queue(GOODS_DOWN_QUEUE);
    }

    //声明交换机
    @Bean(RABBIT_EXCHANGE)
    public Exchange RABBIT_EXCHANGE() {
        //durable(true) 持久化，mq重启之后交换机还在
        return ExchangeBuilder.fanoutExchange(RABBIT_EXCHANGE).durable(true).build();
    }

  @Bean(GOODS_EXCHANGE)
    public Exchange GOODS_EXCHANGE() {
        //durable(true) 持久化，mq重启之后交换机还在
        return ExchangeBuilder.fanoutExchange(GOODS_EXCHANGE).durable(true).build();
    }
 @Bean(GOODS_DOWN_EXCHANGE)
    public Exchange GOODS_DOWN_EXCHANGE() {
        //durable(true) 持久化，mq重启之后交换机还在
        return ExchangeBuilder.fanoutExchange(GOODS_DOWN_EXCHANGE).durable(true).build();
    }

    //队列绑定交换机，指定routingKey
    @Bean
    public Binding RABBIT_EXCHANGE_QUEUE(@Qualifier(RABBIT_QUEUE) Queue queue, @Qualifier(RABBIT_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RABBIT_QUEUE).noargs();
    }
@Bean
    public Binding GOODS_EXCHANGE_QUEUE(@Qualifier(GOODS_QUEUE) Queue queue, @Qualifier(GOODS_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(GOODS_QUEUE).noargs();
    }
@Bean
    public Binding GOODS_EXCHANGE_QUEUE2(@Qualifier(GOODS_DOWN_QUEUE) Queue queue, @Qualifier(GOODS_DOWN_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(GOODS_DOWN_QUEUE).noargs();
    }
}

